home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / castools.zip / ADDENTRY.C < prev    next >
Text File  |  1990-02-12  |  4KB  |  138 lines

  1.  
  2.  
  3. /*
  4.    ADDENTRY.C Function PbAddEntry:  Adds an entry (individual or group) to an
  5.       open phonebook.
  6.  
  7.    INPUT:  Phonebook structure and filled phonebook entry structure.
  8.  
  9.    OUTPUT: If successful, updates the entries field of the phonebook
  10.       structure, and sets the ID field of the entry structure.
  11. */
  12.  
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <ctype.h>
  16. #include <phonebk.h>
  17.  
  18. int pascal PbAddEntry(PB *pb, PBE *entry)
  19. {
  20.   int i;
  21.   PBEFIXED fixed_part;
  22.   char *entry_buffer = NULL;    /* entry that actually gets written to file */
  23.   int filenum;                  /* for call to fileno() */
  24.   long PbSize;
  25.   long offset;
  26.   int buffed_rids;
  27.   int rid;
  28.   int writ;                /* for return from fwrite() */
  29.   int members = entry->members;
  30.  
  31.   /* Error checks */
  32.  
  33.   Pberrno = 0;                               /* Initially, always reset */
  34.  
  35.   /* First, check params */
  36.   if ((pb == NULL) ||
  37.       (entry == NULL)) {
  38.     Pberrno = INVALIDPARAMETER;
  39.     goto error_out;
  40.   }
  41.  
  42.   /* Find the first RID in the offsets array that is NULL, and save that */
  43.   /* If no free RID is found, the phonebook is full */
  44.   for (i=0; i<MAXENTRIES; i++) {
  45.     if (!OffsetOfEntry(pb, i)) {
  46.       break;
  47.     }
  48.   }
  49.   if ((rid = i) == MAXENTRIES) {
  50.     Pberrno = PHONEBOOKFULL;
  51.     goto error_out;
  52.   }
  53.  
  54.   /* Now that we know the phonebook is NOT full, check the #of entries field */
  55.   if (pb->header.entries >= MAXENTRIES) {
  56.     Pberrno = INCONSISTENTPBH;              /* this is warning only */
  57.   }
  58.  
  59.   /* one part of the entry that must be set is the RID field */
  60.   entry->id = rid;
  61.  
  62.   /* Check the proposed new entry for correctness, returning a formatted */
  63.   /*   data area for an entry to add.  note: membership is nulled */
  64.   if (!(entry_buffer = EntryOkToAdd(pb, entry))) {
  65.     Pberrno = INVALIDENTRY;
  66.     goto error_out;
  67.   }
  68.  
  69.   /* Next, find the offset of the end-of-file , and set a vbl offset to it */
  70.   filenum = fileno(pb->fp);
  71.   PbSize = filelength(filenum);
  72.   if (PbSize == -1L) {
  73.     Pberrno = FILELENGTHERROR;
  74.     goto error_out;
  75.   }
  76.   offset = PbSize;
  77.  
  78.   /* Write the new entry to the end of the file, quitting if an error occurred*/
  79.   if (fseek(pb->fp, 0L, SEEK_END)) {
  80.     Pberrno = FSEEKERROR;
  81.     goto error_out;
  82.   }
  83.   writ = fwrite(entry_buffer, entry->length, 1, pb->fp);
  84.   if (writ != 1) {
  85.     Pberrno = CANTWRITE;
  86.     goto error_out;
  87.   }
  88.  
  89.   /* Update entries field in pb->header and write that int to disk */
  90.   if (Pberrno != INCONSISTENTPBH) {
  91.     pb->header.entries++;
  92.     if (fseek(pb->fp,
  93.               4L,
  94.               SEEK_SET)) {
  95.       Pberrno = FSEEKERROR;
  96.       goto error_out;
  97.     }
  98.     writ = fwrite(&pb->header.entries, sizeof(WORD), 1, pb->fp);
  99.     if (writ != 1) {
  100.       Pberrno = CANTWRITE;
  101.       goto error_out;
  102.     }
  103.   }
  104.  
  105.   /* Update the Obuffer, if there is one, and write that info to disk */
  106.   if (pb->OBuffer) {
  107.  
  108.     /* We can be sure that the rid IS in the buffer, from using OffsetOfEntry */
  109.     pb->OBuffer[rid - pb->FirstOBufferRID] = offset;
  110.   }
  111.   if (fseek(pb->fp, 160L + rid*sizeof(LONGWORD), SEEK_SET)) {
  112.     Pberrno = FSEEKERROR;
  113.     goto error_out;
  114.   }
  115.   writ = fwrite(&offset, sizeof(LONGWORD), 1, pb->fp);
  116.   if (writ != 1) {
  117.     Pberrno = CANTWRITE;
  118.     goto error_out;
  119.   }
  120.  
  121.   /* If group has members already, add them now */
  122.   for (i=0; i<members; i++) {
  123.     if (!(PbAddToGroup(pb, rid, entry->MemberList[i]))) {
  124.       goto error_out;           /* Pberrno should be set by PbAddToGroup() */
  125.     }
  126.   }
  127.  
  128.   /* If we got here w/o falling out, all was well! */
  129.   free(entry_buffer);
  130.   return(SUCCESS);
  131.  
  132. error_out:
  133.   if (entry_buffer) {
  134.     free(entry_buffer);
  135.   }
  136.   return(FAIL);
  137. }
  138.